package com.fei.yygh.cmn.controller;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fei.yygh.cmn.service.DitService;
import com.fei.yygh.model.cmn.Dict;
import com.fei.yygh.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.juli.logging.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static jdk.nashorn.internal.runtime.regexp.joni.Config.log;

@Api("数据字典管理")
@RestController
@CrossOrigin
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Autowired
    private DitService ditService;
    @Autowired
    private BaseMapper baseMapper;


    /**
     * longfei
     * 根据数据ID查询子节点数据
     */
    @ApiOperation("ID查询子数据")
    @GetMapping("/findChildData/{id}")
    public Result findByID(@PathVariable Long id) {
        //findSonData是一个自己创建的方法用来查询子数据
       List<Dict> list = ditService.findSonData(id);
        return Result.ok(list);
    }

    /**
     * longfei
     * 导出数据字典接口
     */
    @GetMapping("/exportData")
    public void exportExcel(HttpServletResponse response) {
        ditService.exportExcelData(response);
    }

    /**
     * longfei
     * 导入excel数据
     */
    @PostMapping("/addExcel")
    public Result addExcel(MultipartFile file) {
        ditService.importExcel(file);
        return Result.ok();
    }

    @GetMapping("/{value}")
    public String getNameByValue(@PathVariable("value") Long value) {
        return ditService.getNameByValue(value);
    }

    @GetMapping("/{dictCode}/{value}")
    public String getNameByDictCode(@PathVariable("dictCode") String dictCode,
                                    @PathVariable("value") Long value){
        return ditService.getNameByDictCode(dictCode,value);
    }


}
